1452C - Two Brackets - CodeForces Solution


greedy *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
	s=input()
	ro=0
	so=0
	c=0
	for i in range(len(s)):
		if s[i]=='(':
			ro+=1
		elif s[i]=='[':
			so+=1
		elif s[i]==")" and ro>0:
			ro-=1
			c+=1
		elif s[i]==']' and so>0:
			so-=1
			c+=1
	print(c)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
 
int _count(string s, char left, char right)
{
    int balance(0), count(0);
    for (auto c : s)
    {
        if (c == right)
        {
            if (balance > 0)
            {
                --balance;
                ++count;
            }
        }
        else if (c == left)
            ++balance;
    }
    return count;
}

void solve()
{
    string s;
    cin >> s;
    cout << _count(s, '(', ')') + _count(s, '[', ']') << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}
 
//////////////////////////////////////////
//                                      //
//      Implemented by brownfox2k6      //
//                                      //
//////////////////////////////////////////


Comments

Submit
0 Comments
More Questions

1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers